package com.blast.sspai.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.blast.sspai.R;
import com.blast.sspai.fragment.AdvertFragment;
import com.blast.sspai.fragment.ArticleFragment;
import com.blast.sspai.fragment.CommunityFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 主界面
 */
public class MainActivity extends AppCompatActivity implements AdvertFragment.OnFragmentInteractionListener {

    private ArrayList<Map<String, Object>> infos = new ArrayList<Map<String, Object>>();
    /**
     * 抽屉控件
     */
    private DrawerLayout mDrawerLayout;
    /**
     * 侧滑开关
     */
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        View view = LayoutInflater.from(this).inflate(R.layout.left_menu, null);
        layout.addView(view);

        //默认显示文章标题
        setTitle("文章");

        int[] photo = {R.drawable.ic_sliding_article_normal, R.drawable.ic_sliding_article_forum, R.drawable.ic_sliding_setting_normal};
        final String[] title = {"文章", "社区", "设置"};
        ListView listView = (ListView) findViewById(R.id.listView);

        for (int i = 0; i < photo.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("img", photo[i]);
            item.put("title", title[i]);
            infos.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, infos, R.layout.item_list,
                new String[]{"img", "title"},
                new int[]{R.id.photo, R.id.title});
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        //TODO 文章界面
                        manageFragment(new ArticleFragment());
                        break;
                    case 1:
                        //TODO 社区界面
                        //manageFragment(new CommunityFragment());
                        Toast.makeText(MainActivity.this, "暂未开放", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        //TODO 设置界面
                        Intent intent1 = new Intent(MainActivity.this, SettingsActivity.class);
                        startActivity(intent1);
                        break;
                }
                //点击左侧菜单，切换ToolBar的title
                setTitle(title[i]);
                mDrawerLayout.closeDrawers();
            }
        });


        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //进行这步以后 就相当于把ActionBar替换为toolBar
        setSupportActionBar(toolbar);

        if (toolbar != null) {
            //打开返回按钮
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        //将DrawerToggle 放入 ToolBar
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.draw_open, R.string.draw_close);
        //同步toggle状态
        mDrawerToggle.syncState();
        //对抽屉控件进行事件监听 监听的就是toggle的动作 其实
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        //默认加载ArticleFragment
        manageFragment(new ArticleFragment());
    }

    /**
     * 管理Fragment
     *
     * @param fragment 需要进行替换的Fragment
     */
    public void manageFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * Item被点击事件
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
            case R.id.toolbar_search:
                Intent intent = new Intent(this, DiscoverActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 点击头像进入登陆界面
     *
     * @param view
     */
    public void imgLogin(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
